<!--预警信息管理模块-->
<template>
  <div class="warning wrapper">
    <!-- 预警阈值设置 -->
    <div class="threshold-operate">
      <el-card class="threshold-card">
        <div class="threshold-item">
          <span class="threshold-label">黄色预警值：</span>
          <el-input v-model="yellowValue" size="mini" style="width:120px"></el-input>
          <el-button
            size="mini"
            type="primary"
            @click="updateYellow"
            class="save-btn"
          >保存</el-button>
        </div>
        <div class="threshold-item">
          <span class="threshold-label">红色预警值：</span>
          <el-input v-model="redValue" size="mini" style="width:120px"></el-input>
          <el-button
            size="mini"
            type="primary"
            @click="updateRed"
            class="save-btn"
          >保存</el-button>
        </div>
      </el-card>
    </div>

    <!-- 预警信息列表 -->
    <div class="bd">
      <el-table
        :data="warningList"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        v-loading="loading"
      >
        <el-table-column prop="time" label="时间" sortable width="180"></el-table-column>
        <el-table-column prop="holeLocationId" label="孔号" width="120"></el-table-column>
        <el-table-column prop="waterChange" label="水位变化(m)" width="120"></el-table-column>
        <el-table-column prop="dangerType" label="预警类型" width="120">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.dangerType === 1 ? 'danger' : 'warning'"
              effect="plain"
            >
              {{ scope.row.dangerType === 1 ? '红色预警' : '黄色预警' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row.waterId)"
              class="delete-btn"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      yellowValue: '',
      redValue: '',
      warningList: [],
      loading: false
    }
  },
  created() {
    this.getThreshold()
    this.getWarningList()
  },
  methods: {
    // 获取预警阈值
    async getThreshold() {
      try {
        const res = await axios.get('http://localhost:8080/water-danger/gets')
        if (res.data.code === '0') {
          this.yellowValue = res.data.data[0].dangerValueYellow
          this.redValue = res.data.data[0].dangerValueRed
        }
      } catch (error) {
        this.$message.error('获取阈值失败')
      }
    },

    // 更新黄色预警
    async updateYellow() {
      try {
        const res = await axios.post('http://localhost:8080/water-danger/updateYellow', {
          dangerValueYellow: this.yellowValue
        })
        if (res.data.code === '0') {
          this.$message.success('更新成功')
          this.getThreshold()
        }
      } catch (error) {
        this.$message.error('更新失败')
      }
    },

    // 更新红色预警
    async updateRed() {
      try {
        const res = await axios.post('http://localhost:8080/water-danger/updateRed', {
          dangerValueRed: this.redValue
        })
        if (res.data.code === '0') {
          this.$message.success('更新成功')
          this.getThreshold()
        }
      } catch (error) {
        this.$message.error('更新失败')
      }
    },

    // 获取预警列表
    async getWarningList() {
      this.loading = true
      try {
        const res = await axios.get('http://localhost:8080/water-danger-info/getAll')
        if (res.data.code === '0') {
          this.warningList = res.data.data.sort((a, b) =>
            new Date(b.time) - new Date(a.time))
        }
      } catch (error) {
        this.$message.error('获取列表失败')
      } finally {
        this.loading = false
      }
    },

    // 删除预警
    handleDelete(waterId) {
      this.$confirm('确定删除该预警信息吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          await axios.delete(`http://localhost:8080/water-danger-info/delete/${waterId}`)
          this.$message.success('删除成功')
          this.getWarningList()
        } catch (error) {
          this.$message.error('删除失败')
        }
      })
    },

    // 表格行样式
    tableRowClassName({ row }) {
      return row.dangerType === '1' ? 'warning-row-red' : 'warning-row-yellow'
    }
  }
}
</script>

<style scoped>
.warning {
  padding: 20px;
}

.threshold-operate {
  margin-bottom: 20px;
}

.threshold-card {
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.threshold-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.threshold-item:last-child {
  margin-bottom: 0;
}

.threshold-label {
  width: 100px;
  font-size: 14px;
  color: #606266;
}

.save-btn {
  margin-left: 10px;
}

.delete-btn {
  color: #f56c6c;
}

.delete-btn:hover {
  color: #f78989;
}

/* 表格行样式 */
:deep(.warning-row-red) td {
  background-color: #fff0f0 !important;
}

:deep(.warning-row-yellow) td {
  background-color: #fff9e6 !important;
}

:deep(.el-table__row:hover td) {
  background-color: #f5f7fa !important;
  transform: translateY(-2px);
  transition: all 0.3s;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}
</style>
